
import { _decorator, Component, Label, Node } from 'cc';
import { DataHelper } from '../system/DataHelper';
import { AppControll, ConfigKey, PopView } from '../manager/AppControll';
import { GameData } from '../system/GameData';
import { PopManager } from '../manager/PopManager';
import { SystemMessage } from './SystemMessage';
import { SoundManager } from '../manager/SoundManger';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = NewComponent
 * DateTime = Wed May 24 2023 23:16:05 GMT+0800 (GMT+08:00)
 * Author = guoxun
 * FileBasename = NewComponent.ts
 * FileBasenameNoExtension = NewComponent
 * URL = db://assets/script/pop/NewComponent.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('GetSecondGun')
export class GetSecondGun extends Component {
    @property(Node)
    mNdFree: Node = null;
    @property(Node)
    mNdGuide: Node = null;
    @property(Label)
    mLbSpend: Label = null;
    @property(Node)
    mNdSpend: Node = null;
    @property(Label)
    mLbContent: Label = null;

    private mCallBack = null;
    private mIsFree = false;

    protected onLoad(): void {

    }

    InitView(callBack: () => void) {
        this.mCallBack = callBack;
        let spend = DataHelper.GetConfigByKey(ConfigKey.buySecondGun).value;
        let str = `是否花费${spend}金币随机一个炮塔？`;
        this.mLbContent.string = str;

        let cont = 0;
        GameData.playerData.SecondaryGun.forEach(elemt => {
            if (elemt) {
                cont++;
            }
        })
        let isFree = cont == 0;
        this.mIsFree = isFree;
        this.mLbSpend.string = spend;
        if (isFree) {
            this.mNdFree.active = true;
            this.mNdGuide.active = true;
            this.mLbSpend.node.active = false;
            this.mNdSpend.active = false;
        } else {
            this.mNdFree.active = false;
            this.mNdGuide.active = false;
            this.mLbSpend.node.active = true;
            this.mNdSpend.active = true;
        }
    }

    OnBtnConfirm() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        let price = parseInt(DataHelper.GetConfigByKey(ConfigKey.buySecondGun).value);
        if (GameData.playerData.Gold >= price) {
            GameData.AddGold(price * -1);
            if (this.mCallBack) this.mCallBack();
            this.node.active = false;
        } else if (this.mIsFree) {
            AppControll.Platform.SendBuryPoint(100015);
            if (this.mCallBack) this.mCallBack();
            this.node.active = false;
        }
        else {
            PopManager.Instance<PopManager>().ShowPop(PopView.systemMessage, (node) => {
                node.getComponent(SystemMessage).InitContent("金币不足");
            })
        }
    }

    OnClose() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        this.node.active = false;
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
